home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1996 July / macformat-039.iso / Shareware City / Graphics / Clut Converter / Source / PShopPal.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-01-29  |  2.9 KB  |  102 lines  |  [TEXT/CWIE]

  1. /*
  2.  
  3. "PShopPal v1.0"
  4. by Reevan McKay
  5. ===============
  6.  
  7. DESCRIPTION:
  8. ============
  9. This is a C routine allowing your application to create color tables from
  10. the data stored in AdobePhotoShop 2.5 & 3.0 clut files.
  11.  
  12. This routine is "freeware".  You may use it, in any form, in any application,
  13. without condition and without credit.  If you find it useful, you might let the
  14. author know at:
  15.  
  16. rmckay@chat.carleton.ca
  17.  
  18.  
  19. USAGE:
  20. ======
  21.  
  22.     OSErr        LoadPalette (FSSpec theFSpec, CTabHandle    theCtab, 
  23.                             unsigned char position, unsigned char colorCount);
  24.  
  25.     theFSpec - a FSSpec pointing to the file you want to open
  26.     theCTab - a CTabHandle to an initialized ColorTable.  
  27.                 Note that LoadPalette DOES NOT INITIALIZE A COLOR TABLE!
  28.     position - number of entries in the CTabHandle to skip.
  29.     colorCount - number of entries to read from the Photoshop file.
  30.  
  31. OSErr should  == 0L  if all goes well.  If there is an error, the color table you
  32.                         received may contain incorrect data.
  33.  
  34. /!\    LoadPalette will crash or do weird things if you pass an invalid CTabHandle.
  35. I suggest initializing one with GetCTable (8).  This will create a greyscale colortable
  36. which you can then modify the entries of.  The nice thing about this is that it is
  37. possible to use several calls to LoadPalette to fill a colortable with data from
  38. several different files (such as 128 colors from back.pal and another 128 from sprite.pal).
  39. Also, this routine ASSUMES that you are passing a valid FSSpec to a valid PhotoShop CLUT file,
  40. in "theFSpec".  If you do not, you may encounter abnormal results or unexpected hangs and/or
  41. crashes, sunspots or earthquakes :p
  42.  
  43. This routine carries no guarantee as to its fitness for any particular purpose.
  44. Author is not responsible for any damage to data or hardware arising from use of this routine.
  45.  
  46. "Checked and cleared. . . Have a better one. . ."
  47. - Spinner Officer, BladeRunner
  48.  
  49.  
  50. */
  51.  
  52. #include "PShopPal.h"
  53. #include <pictutils.h>
  54. #ifndef NIL
  55. #define NIL    0L
  56. #endif
  57.  
  58.  
  59. OSErr        LoadPalette (FSSpec theFSpec,CTabHandle    theCtab,unsigned char position,
  60.                                     unsigned char colorCount)
  61. {
  62.     OSErr                    Err    =    NIL;
  63.     short                    refNum;
  64.     Ptr                        bufferPtr;
  65.     long                    count;
  66.     short                    loop;
  67.     long                    currentPos;
  68.     
  69.     Err    =    FSpOpenDF (&theFSpec,fsCurPerm,&refNum);
  70.         if(Err)
  71.             return Err;
  72.  
  73.     Err    =    SetFPos (refNum,fsFromStart,0);
  74.         if(Err)
  75.             return Err;
  76.  
  77.     bufferPtr=NewPtr (6);
  78.     
  79.         for (loop=0;loop<colorCount;loop++)
  80.             {
  81.                 count    =    1;
  82.                 Err    =    FSRead(refNum,&count, bufferPtr);
  83.                 (*theCtab)->ctTable[position].rgb.red        =    (*((char*)bufferPtr))<<8;
  84.  
  85.                 count    =    1;
  86.                 Err    =    FSRead(refNum,&count, bufferPtr);
  87.                 (*theCtab)->ctTable[position].rgb.green        =    (*((char*)bufferPtr))<<8;
  88.  
  89.                 count    =    1;
  90.                 Err    =    FSRead(refNum,&count, bufferPtr);
  91.                 (*theCtab)->ctTable[position].rgb.blue        =    (*((char*)bufferPtr))<<8;
  92.                 
  93.                 position++;
  94.             };
  95.             
  96.     Err = FSClose (refNum);
  97.         if(Err)
  98.             return Err;
  99.     DisposePtr (bufferPtr);
  100.     return NIL;
  101. }
  102.